package atguigu.web;
import atguigu.pojo.Book;
import atguigu.pojo.Page;
import atguigu.service.BookService;
import atguigu.service.impl.BookServiceImpl;
import atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BookServlet extends BaseServlet{
    private  BookService bookService = new BookServiceImpl();
    //处理分页功能
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的参数 pageNo,PageSize  默认为首页1
        String pages = req.getParameter("pageNo");
        int pageNo = WebUtils.parseInt(pages,1);
        //默认为显示4条记录
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //2.调用BookService.page(pageNo,pageSize)方法  Page对象
        Page<Book> page = bookService.page(pageNo,pageSize);
        //设置后台页面 分页的访问地址
        page.setUrl("manager/bookServlet?action=page");
        //3.保存Page对象到Request域中
        req.setAttribute("page",page);
        //4.请求转发到pages/manager/book_manager.jsp页面
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }
    //增加
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"),0);
         pageNo+=1;
        //1获取请求的参数  封装成为Book对象
        Book book= WebUtils.copyParamToBean(req.getParameterMap(),new Book());
        //2.调用BookService.addBook保存图书
        bookService.addBook(book);
        //3.跳转到图书列表页面 但是这种方法有一个bug  按f5会重复提交
        //req.getRequestDispatcher("/manager/bookServlet?action=query").forward(req,resp);
        //所以用重定向方法
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo="+pageNo);
    }
    //删除
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1获取Id参数
        String id = req.getParameter("id");
        //使用工具中的类型转换
        int Id = WebUtils.parseInt(id,0);
        //2.调用BookService.deleteBookBiId保存图书
        bookService.deleteBookById(Id);
        //用重定向方法
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }
    //保存修改后的信息 然后显示到book_manager.jsp页面
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的参数 封装成为Book对象
        Book book = WebUtils.copyParamToBean(req.getParameterMap(),new Book());
        //2.调用BookService.updateBook()方法
        bookService.updateBook(book);
        //3.重定向到图书列表 显示修改后的信息
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));

    }
    //查询
    protected void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1通过BookService查询全部图书
        List<Book> books=bookService.queryBooks();
        //2把全部图书保存到Request域中
        req.setAttribute("books",books);
        //3请求转发到/pages/manager/book_manager.jsp页面
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }
    //点击修改图书  此时把点击的信息跳转到该方法  进行保存 再进行跳转到修改页面显示
    protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的参数图书编号
        String id = req.getParameter("id");
        int Id = WebUtils.parseInt(id,0);
        //2调用bookService.queryBookBiId()方法
        Book book= bookService.queryBookById(Id);
        //3.保存到Request域中
        req.setAttribute("book",book);
        //4.请求转发到pages/manager/book_edit.jsp页面
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
    }
}
